package uw10.app;

import uw10.app.data.Event;
import uw10.app.data.sources.impl.RandomEventGraphDataSource;
import uw10.app.display.EventGraphView;
import uw10.app.display.EventIconFactory;
import uw10.app.display.EventIconRenderer;
import uw10.app.display.EventRaterListener;
import uw10.app.display.EventRaterView;
import uw10.app.display.EventSelectListener;
import uw10.app.display.EventSelectView;
import uw10.app.display.GraphRenderer;
import uw10.app.display.RatingColorFactory;
import android.app.Activity;
import android.app.Notification;
import android.app.TimePickerDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;

public class MainActivity extends Activity implements EventSelectListener, EventRaterListener
{
	private static final Event THRESHOLD_EVENT = new Event(0, 0, 0, 0, 0, 0);
	
	EventSelectView esv;
	EventGraphView egv;
	EventRaterView erv;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        bindMainService();
        
        setContentView(R.layout.main);
        
        esv = (EventSelectView)findViewById(R.id.eventselectview);
        egv = (EventGraphView)findViewById(R.id.eventgraphview);
        erv = (EventRaterView)findViewById(R.id.eventraterview);
        
        Event[] events = new Event[15];
        for (int i = 0; i < events.length; i++)
        {
        	events[i] = new Event(Event.NEW, i, 0, (int)(Math.random() * 255), i * 3, i * 7);
        }
        
        Bitmap[] icons = { BitmapFactory.decodeResource(esv.context.getResources(), R.drawable.icon, null) };
        
        Bitmap slider = BitmapFactory.decodeResource(esv.context.getResources(), R.drawable.slider, null);
        
        Bitmap rater = BitmapFactory.decodeResource(erv.context.getResources(), R.drawable.rater, null);
        
        int maxRating = 255;
        RatingColorFactory rcf = new RatingColorFactory(maxRating); 
        
        esv.setup(6, new EventIconRenderer(new EventIconFactory(icons), rcf), slider);
        esv.setEvents(events);
        
        egv.setup(new GraphRenderer(), new RandomEventGraphDataSource());
        
        erv.setup(true, maxRating, rcf, 32, rater);
        
        esv.setListener(this);
        erv.setListener(this);
        
		EventRaterView threshold = (EventRaterView)findViewById(R.id.thresholdview);
		threshold.setup(false, maxRating, rcf, 32, BitmapFactory.decodeResource(threshold.context.getResources(), R.drawable.rater, null));
		threshold.setListener(this);
    }
    
    public void playAlert()
    {
    	Notification notification = new Notification(android.R.drawable.ic_popup_reminder,
            "My Notification", System.currentTimeMillis());
    	notification.defaults |= Notification.DEFAULT_SOUND;
    	notification.defaults |= Notification.DEFAULT_VIBRATE;
    }
    
	public void onDestroy()
	{
	    super.onDestroy();
	    unbindMainService();
	}

	public void onEventSelected(Event e)
	{
		egv.setEvent(e);
		erv.setEvent(e);
		findViewById(R.id.alerttext).setVisibility(View.GONE);
		findViewById(R.id.alertimg).setVisibility(View.GONE);
        findViewById(R.id.eventbuttons).setVisibility(View.VISIBLE);
        onButtonGraph(null);
	}
	
	private MainService mainService;
	
	private ServiceConnection msc = new ServiceConnection()
	{
		public void onServiceConnected(ComponentName className, IBinder service)
	    {
	    	mainService = ((MainService.MainServiceBinder)service).getService();
	    }

	    public void onServiceDisconnected(ComponentName className)
	    {
	    	mainService = null;
	    }
	};
	
	private void bindMainService()
	{
		bindService(new Intent(this, MainService.class), msc, Context.BIND_AUTO_CREATE);
		startService(new Intent(this, MainService.class));
	}
	
	private void unbindMainService()
	{
		if (mainService != null)
		{
			unbindService(msc);
		}
	}

	public void onEventRatingSelected(Event event, int rating)
	{
		if (event == THRESHOLD_EVENT)
		{
			event.rating = rating;
		}
		else
		{
			Log.w("Rating", "New rating " + rating);
		}
	}
	
	public void onButtonDiscard(View view)
	{
		
	}
	
	public void onButtonStar(View view)
	{
		
	}
	
	public void onButtonSettings(View view)
	{
		if (mainService != null)
		{
			ToggleButton toggle = ((ToggleButton)findViewById(R.id.servicetoggle));
			toggle.setChecked(mainService.getActive());
			
			TimePicker time = ((TimePicker)findViewById(R.id.mytimepicker));
			time.setIs24HourView(true);
			long period = mainService.getPeriod();
			time.setCurrentHour((int) (period / (60 * 60000)));
			time.setCurrentMinute((int) (period / 60000) % 60);
			
			((EditText)findViewById(R.id.servicehost)).setText(mainService.getHost(), TextView.BufferType.EDITABLE);

			THRESHOLD_EVENT.rating = mainService.getThreshold();
			((EventRaterView)findViewById(R.id.thresholdview)).setEvent(THRESHOLD_EVENT);

			findViewById(R.id.nosettings).setVisibility(View.GONE);
			findViewById(R.id.settings).setVisibility(View.VISIBLE);
		}
	}
	
	public void onButtonSaveSettings(View view)
	{
		if (mainService != null)
		{
			// save settings
			mainService.setThreshold(THRESHOLD_EVENT.rating);
			
			TimePicker time = ((TimePicker)findViewById(R.id.mytimepicker));
			long period = (time.getCurrentHour() * 60 + time.getCurrentMinute()) * 60000;
			if (period == 0) period = 24 * 60 * 60000;
			mainService.setPeriod(period);
			
			String host = ((EditText)findViewById(R.id.servicehost)).getText().toString();
			mainService.setHost(host);
			
			ToggleButton toggle = ((ToggleButton)findViewById(R.id.servicetoggle));
			mainService.setActive(toggle.isChecked());
			
			findViewById(R.id.nosettings).setVisibility(View.VISIBLE);
			findViewById(R.id.settings).setVisibility(View.GONE);
		}
	}
	
	public void onButtonInfo(View view)
	{
		findViewById(R.id.eventinfo).setVisibility(View.VISIBLE);
		findViewById(R.id.eventview).setVisibility(View.GONE);
		findViewById(R.id.buttongraph).setVisibility(View.VISIBLE);
		findViewById(R.id.buttoninfo).setVisibility(View.GONE);
	}
	
	public void onButtonGraph(View view)
	{
		findViewById(R.id.eventinfo).setVisibility(View.GONE);
		findViewById(R.id.eventview).setVisibility(View.VISIBLE);
		findViewById(R.id.buttongraph).setVisibility(View.GONE);
		findViewById(R.id.buttoninfo).setVisibility(View.VISIBLE);
	}
}